package com.kara.woodAgent.agent.graph.impl;

import com.kara.woodAgent.agent.graph.Next;
import com.kara.woodAgent.agent.graph.Node;
import com.kara.woodAgent.agent.tool.provider.ToolProvider;
import dev.langchain4j.agent.tool.ToolExecutionRequest;
import dev.langchain4j.data.message.ToolExecutionResultMessage;

import java.util.List;

/**
 * @author tzy
 * @version 1.0
 * @project wood_agent
 * @description
 * @date 2025/6/4 11:26:12
 */
public class ToolNode implements Node<ReactGraphContext> {

	private final ToolProvider.ToolServiceHolder holder;


	public ToolNode(ToolProvider.ToolServiceHolder holder) {
		this.holder = holder;
	}


	@Override
	public Next execute(ReactGraphContext context) {


		List<ToolExecutionRequest> toolExecutionRequest = context.getToolExecutionRequest();
		String memoryId = context.getMemoryId();

		List<ToolExecutionResultMessage> resultMessages = holder.executeTool(memoryId, toolExecutionRequest);

		for (ToolExecutionResultMessage resultMessage : resultMessages) {
			context.getChatMessages().add(resultMessage);
		}
		context.setToolExecutionRequest(null);

		return Next.NextNode("model").step(toolExecutionRequest.toString()+"   "+resultMessages.toString());
	}
}
